package com.furongji.education.controller.admin;

import com.furongji.common.result.Result;
import com.furongji.education.domain.po.Chapter;
import com.furongji.education.domain.vo.ChapterVo;
import com.furongji.education.service.ChapterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-19
 */
@Tag(name = "课程章节管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/courses")
public class ChapterAdminController {

    private final ChapterService chapterService;

    @Operation(summary = "获取课程的章节小节")
    @GetMapping("/getChapterAndSection/{courseId}")
    public Result getChapterAndSection(@PathVariable String courseId) {
        List<ChapterVo> chapterVoList = chapterService.getChapterAndSection(courseId);
        return Result.success(chapterVoList);
    }

    //查询章节
    @Operation(summary = "根据id查询章节")
    @GetMapping("getChapter/{id}")
    public Result getChapter(@PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return Result.success(chapter);
    }

    //添加章节
    @Operation(summary = "添加章节")
    @PostMapping("addChapter")
    public Result addChapter(@RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if(save){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    //修改章节
    @Operation(summary = "修改章节")
    @PutMapping("updateChapter")
    public Result updateChapter(@RequestBody Chapter chapter){
        boolean update = chapterService.updateById(chapter);
        if(update){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    //删除章节
    @Operation(summary = "删除章节")
    @DeleteMapping("delChapter/{id}")
    public Result delChapter(@PathVariable String id){
        Boolean flag = chapterService.deleteChapter(id);
        if (flag){
            return Result.success();
        }else{
            return Result.error();
        }
    }
}
